package com.xc.os.webservice.consumer;

import com.xc.os.webservice.consumer.webservice.ApiService;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;

/**
 * @author xuweijie
 * @version 1.0
 * @classname CxfClient
 * @description
 * @date 2021/01/15 14:26
 */
public class CxfClient {
    public static void main(String[] args) {
        main1();
    }
    /**
     * 1.代理类工厂的方式,需要拿到对方的接口地址
     */
    public static void main1 () {
        try {
            // 接口地址
            String address = "http://127.0.0.1:8088/services/ws/api?wsdl";
            // 代理工厂
            JaxWsProxyFactoryBean jaxWsProxyFactoryBean = new JaxWsProxyFactoryBean();
            // 设置代理地址
            jaxWsProxyFactoryBean.setAddress(address);
            // 设置接口类型
            jaxWsProxyFactoryBean.setServiceClass(ApiService.class);
            // 创建一个代理接口实现
            ApiService us = (ApiService) jaxWsProxyFactoryBean.create();
            // 调用代理接口的方法调用并返回结果
            String result = us.emrService("用户id的查询，");
            System.out.println("返回结果:" + result);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 2：动态调用
     */
    public static void main2 () {
        // 创建动态客户端
        JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
        Client client = dcf.createClient("http://127.0.0.1:8088/services/ws/api?wsdl");
        // 需要密码的情况需要加上用户名和密码
        // client.getOutInterceptors().add(new ClientLoginInterceptor(USER_NAME, PASS_WORD));
        Object[] objects = new Object[0];
        try {
            // invoke("方法名",参数1,参数2,参数3....);
            objects = client.invoke("emrService", "李四");
            System.out.println("返回数据:" + objects[0]);
        } catch (java.lang.Exception e) {
            e.printStackTrace();
        }
    }
}